#include "mish_custom_tiling.h"
#include "kernel_operator.h"

constexpr int32_t BUFFER_NUM = 2; // tensor num for each queue

class KernelMish {
public:
    __aicore__ inline KernelMish() {}
    __aicore__ inline void Init(GM_ADDR x, GM_ADDR z, uint32_t totalLength, uint32_t tileNum)
    {
        this->blockLength = totalLength / AscendC::GetBlockNum();
        this->tileNum = tileNum;
        this->tileLength = this->blockLength / tileNum / BUFFER_NUM;
        xGm.SetGlobalBuffer((__gm__ float *)x + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
        zGm.SetGlobalBuffer((__gm__ float *)z + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
        pipe.InitBuffer(inQueueX, BUFFER_NUM, this->tileLength * sizeof(float));
        pipe.InitBuffer(outQueueZ, BUFFER_NUM, this->tileLength * sizeof(float));
        pipe.InitBuffer(tmpBuf0, this->tileLength * sizeof(float));
        pipe.InitBuffer(tmpBuf1, this->tileLength * sizeof(float));
        pipe.InitBuffer(tmpBuf2, this->tileLength * sizeof(float));
        pipe.InitBuffer(tmpBuf3, this->tileLength * sizeof(float));
        pipe.InitBuffer(tmpBuf4, this->tileLength * sizeof(float));
        pipe.InitBuffer(tmpBuf5, this->tileLength * sizeof(float));
        pipe.InitBuffer(tmpBuf6, this->tileLength * sizeof(float));
        pipe.InitBuffer(tmpBuf7, this->tileLength * sizeof(float));
    }
    __aicore__ inline void Process()
    {
        int32_t loopCount = this->tileNum * BUFFER_NUM;
        for (int32_t i = 0; i < loopCount; i++) {
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(int32_t progress)
    {
        AscendC::LocalTensor<float> xLocal = inQueueX.AllocTensor<float>();
        AscendC::DataCopy(xLocal, xGm[progress * this->tileLength], this->tileLength);
        inQueueX.EnQue(xLocal);
    }
    __aicore__ inline void Compute(int32_t progress)
    {
        AscendC::LocalTensor<float> xLocal = inQueueX.DeQue<float>();
        AscendC::LocalTensor<float> zLocal = outQueueZ.AllocTensor<float>();
        AscendC::LocalTensor<float> tmp0 = tmpBuf0.Get<float>();
        AscendC::LocalTensor<float> tmp1 = tmpBuf1.Get<float>();
        AscendC::LocalTensor<float> tmp2 = tmpBuf2.Get<float>();
        AscendC::LocalTensor<float> tmp3 = tmpBuf3.Get<float>(); 
        AscendC::LocalTensor<float> tmp4 = tmpBuf4.Get<float>(); 
        AscendC::LocalTensor<float> tmp5 = tmpBuf5.Get<float>(); 
        AscendC::LocalTensor<float> tmp6 = tmpBuf6.Get<float>();
        AscendC::LocalTensor<float> tmp7 = tmpBuf7.Get<float>();

        uint8_t  repeat = (uint8_t)tileLength / 64;

        // z = x · tanh( ln( 1 + e ^ {x} ) )
        // ln( 1 + e ^ {x} )  ->  ln(e^0 + e^x) = max(0,x) + ln(1 + e^(-|x|))
        //  tmp0 = max(0, x)
        AscendC::Maxs(tmp0, xLocal, (float)0.0, 64, repeat, { 1, 1, 8, 8 });

        // tmp1 = | x |
        AscendC::Abs(tmp1, xLocal, 64, repeat, { 1, 1, 8, 8 });
        // tmp2 = - | x |
        AscendC::Muls(tmp2, tmp1, (float)(-1.0), 64, repeat, { 1, 1, 8, 8 });
        // tmp3 = e ^ ( - | x | )
        AscendC::Exp(tmp3, tmp2, this->tileLength);
        // tmp4 = 1 +  e ^ ( - | x | )
        AscendC::Adds(tmp4, tmp3, (float)1.0, 64, repeat, { 1, 1, 8, 8 });
        // tmp5 = ln(1 + e^(-|x|))
        AscendC::Ln(tmp5, tmp4, 64, repeat, { 1, 1, 8, 8 });

        // tmp6 = max(0,x) + ln(1 + e^(-|-x|))
        AscendC::Add(tmp6, tmp5, tmp0, 64, repeat, { 1, 1, 1, 8, 8, 8 });

        // tmp7 = tanh( tmp6 )
        AscendC::Tanh(tmp7, tmp6, this->tileLength);

        // z = x · tanh( ln( 1 + e ^ {x} ) )
        AscendC::Mul(zLocal, tmp7, xLocal, 64, repeat, { 1, 1, 1, 8, 8, 8 });

        outQueueZ.EnQue<float>(zLocal);
        inQueueX.FreeTensor(xLocal);
        
    }
    __aicore__ inline void CopyOut(int32_t progress)
    {
        AscendC::LocalTensor<float> zLocal = outQueueZ.DeQue<float>();
        AscendC::DataCopy(zGm[progress * this->tileLength], zLocal, this->tileLength);
        outQueueZ.FreeTensor(zLocal);
    }

private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueX;
    AscendC::TQue<AscendC::TPosition::VECOUT, BUFFER_NUM> outQueueZ;
    AscendC::TBuf<AscendC::QuePosition::VECCALC> tmpBuf0, tmpBuf1, tmpBuf2, tmpBuf3, tmpBuf4, tmpBuf5, tmpBuf6, tmpBuf7;
    AscendC::GlobalTensor<float> xGm;
    AscendC::GlobalTensor<float> zGm;
    uint32_t blockLength;
    uint32_t tileNum;
    uint32_t tileLength;
};

extern "C" __global__ __aicore__ void mish_custom(GM_ADDR x, GM_ADDR z, MishCustomTilingData tiling)
{
    KernelMish op;
    op.Init(x, z, tiling.totalLength, tiling.tileNum);
    op.Process();
}
